package com.kuqi.utils;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import com.kuqi.R;
import com.kuqi.core.ApplicationPersistent;


public class DialogCreator {

    /**
     * 创建一个普通对话框（包含确定、取消按键）
     * @param context
     * @param title
     * @param mesage
     * @param cancelable       是否允许返回键取消
     * @param positiveListener 确定按键动作
     * @param negativeListener 取消按键动作
     * @return
     */
    public static AlertDialog createCommonDialog(Context context, String title, String mesage, boolean cancelable,
                                                 DialogInterface.OnClickListener positiveListener, DialogInterface.OnClickListener negativeListener) {
        final AlertDialog.Builder normalDialog = new AlertDialog.Builder(context);
        normalDialog.setTitle(title);
        normalDialog.setCancelable(cancelable);
        normalDialog.setMessage(mesage);
        normalDialog.setPositiveButton("确定", positiveListener);
        normalDialog.setNegativeButton("取消", negativeListener);
        final AlertDialog alertDialog = normalDialog.create();
        ApplicationPersistent.getApplication().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                try {
                    alertDialog.show();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        return alertDialog;
    }



    /**
     * 阅读设置对话框
     *
     * @param context
     * @param isDayStyle
     * @param chapterProgress
     * @param backListener
     * @param lastChapterListener
     * @param nextChapterListener
     * @param chapterListListener
     * @param onClickNightAndDayListener
     * @param settingListener
     * @return
     */
    public static Dialog createReadSetting(final Context context, final boolean isDayStyle, int chapterProgress,
                                           View.OnClickListener backListener,
                                           View.OnClickListener lastChapterListener,
                                           View.OnClickListener nextChapterListener,
                                           View.OnClickListener chapterListListener,
                                           final OnClickNightAndDayListener onClickNightAndDayListener,
                                           View.OnClickListener settingListener,
                                           SeekBar.OnSeekBarChangeListener onSeekBarChangeListener,
                                           View.OnClickListener voiceOnClickListener,
                                           final OnClickDownloadAllChapterListener onClickDownloadAllChapterListener) {
        final Dialog dialog = new Dialog(context, R.style.default_dialog_style);
        final View view = LayoutInflater.from(context).inflate(R.layout.dialog_read_setting, null);
        dialog.setContentView(view);
        LinearLayout llBack = (LinearLayout) view.findViewById(R.id.ll_title_back);
        TextView tvLastChapter = (TextView) view.findViewById(R.id.tv_last_chapter);
        TextView tvNextChapter = (TextView) view.findViewById(R.id.tv_next_chapter);
        SeekBar sbChapterProgress = (SeekBar) view.findViewById(R.id.sb_read_chapter_progress);
        LinearLayout llChapterList = (LinearLayout) view.findViewById(R.id.ll_chapter_list);
        LinearLayout llNightAndDay = (LinearLayout) view.findViewById(R.id.ll_night_and_day);
        LinearLayout llSetting = (LinearLayout) view.findViewById(R.id.ll_setting);
        final ImageView ivNightAndDay = (ImageView) view.findViewById(R.id.iv_night_and_day);
        final TextView tvNightAndDay = (TextView) view.findViewById(R.id.tv_night_and_day);
        ImageView ivVoice  = (ImageView)view.findViewById(R.id.iv_voice_read);

        view.findViewById(R.id.rl_title_view).setOnClickListener(null);
        view.findViewById(R.id.ll_bottom_view).setOnClickListener(null);

        Window window = dialog.getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        if (Build.VERSION.SDK_INT >= 21) {
            window.setStatusBarColor(dialog.getContext().getResources().getColor(R.color.sys_dialog_setting_bg));
        }

        view.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                dialog.dismiss();
                return false;
            }
        });
        if (!isDayStyle) {
            ivNightAndDay.setImageResource(R.mipmap.z4);
            tvNightAndDay.setText(context.getString(R.string.day));
        }

        llBack.setOnClickListener(backListener);
        tvLastChapter.setOnClickListener(lastChapterListener);
        tvNextChapter.setOnClickListener(nextChapterListener);
        sbChapterProgress.setProgress(chapterProgress);
        llChapterList.setOnClickListener(chapterListListener);
        llSetting.setOnClickListener(settingListener);
        sbChapterProgress.setOnSeekBarChangeListener(onSeekBarChangeListener);
        ivVoice.setOnClickListener(voiceOnClickListener);
        //日夜切换
        llNightAndDay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                boolean isDay;
                if (tvNightAndDay.getText().toString().equals(context.getString(R.string.day))) {
                    isDay = false;
                    ivNightAndDay.setImageResource(R.mipmap.ao);
                    tvNightAndDay.setText(context.getString(R.string.night));
                } else {
                    isDay = true;
                    ivNightAndDay.setImageResource(R.mipmap.z4);
                    tvNightAndDay.setText(context.getString(R.string.day));
                }
                if (onClickNightAndDayListener != null) {
                    onClickNightAndDayListener.onClick(dialog, view, isDay);
                }
            }
        });

        //缓存全部章节
        final TextView tvDownloadProgress = (TextView)view.findViewById(R.id.tv_download_progress);
        LinearLayout llDonwloadCache = (LinearLayout)view.findViewById(R.id.ll_download_cache);
        llDonwloadCache.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onClickDownloadAllChapterListener != null){
                    onClickDownloadAllChapterListener.onClick(dialog,v,tvDownloadProgress);
                }
            }
        });

        dialog.setCancelable(true);
        dialog.setCanceledOnTouchOutside(true);
        dialog.show();
        return dialog;
    }


    public interface OnClickNightAndDayListener {
        void onClick(Dialog dialog, View view, boolean isDayStyle);
    }

    public interface OnClickDownloadAllChapterListener {
        void onClick(Dialog dialog, View view, TextView tvDownloadProgress);
    }

}
